package rothag.graphics.roulette;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author
 */
public class RouletteJPanel extends JPanel {

    static BufferedImage imagechiffres = null;
    Roulette roulette = new Roulette();
    int Size = 160;

    public RouletteJPanel(int dimension) throws IOException {
        super();

        this.Size = dimension;
        if (imagechiffres == null) {
            imagechiffres = ImageIO.read(getClass().getResource("chiffres_roulette.png"));

            Image k = imagechiffres.getScaledInstance(Size, Size * 11, Image.SCALE_AREA_AVERAGING);
            imagechiffres = toBufferedImage(k);
        }

        initialize();
    }

    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        image = new ImageIcon(image).getImage();

        boolean hasAlpha = true;

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            int transparency = Transparency.TRANSLUCENT;
            if (hasAlpha) {
                transparency = Transparency.BITMASK;
            }

            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            System.err.println(e.getMessage());
        }

        if (bimage == null) {
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB_PRE;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        Graphics g = bimage.createGraphics();

        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }

    private void initialize() {
        Dimension dim = new Dimension(Size, Size);
        this.setSize(Size, Size);
        this.setMaximumSize(dim);
        this.setMinimumSize(dim);
        this.setPreferredSize(dim);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        int V0 = (int) roulette.position - 1;
        float V1 = roulette.position / 1.018f; // Petite correction afin de corriger un décalage.
        float remains = roulette.position - V1;
        g.drawImage(imagechiffres, 0, 0, Size, Size, 0, (int) (0 + V1 * Size) + Size / 8, Size, Size + (int) (0 + V1 * Size) + Size / 8, this);
    }
}
